﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace ProjectFitus.Models
{
    public class RequestEditModel
    {

        public long requestId { get; set; }
        public long requestedByUserId { get; set; }
        public string requestDescription { get; set; }
        public string requestType { get; set; }
        public string requestTitle { get; set; }
        public int statusId { get; set; }
        public List<RequestResponseListItem> responses = null;
        public string requesterUrlText;

        public void InsertIntoModel(Request req, HtmlHelper hlpr)
        {
            requesterUrlText = req.User.userName;
            requestId = req.id;
            requestedByUserId = req.requestedBy;
            requestTitle = req.requestTitle;
            statusId = req.statusId;
            requestDescription = req.requestDescription;
            requestType = req.RequestType1.requestTypeName;
            if (req.RequestResponses.Count != 0)
            {
                this.AddResponses(req.RequestResponses, hlpr);
            }

        }
        private void AddResponses(IEnumerable<RequestResponse> responsesToAdd, HtmlHelper hlpr)
        {
            int requesterResponses = 1;
            int answererResponses = 0;
            responses = new List<RequestResponseListItem>();
            foreach (var item in responsesToAdd)
            {
                RequestResponseListItem reqResp = new RequestResponseListItem();
                reqResp.responseId = item.id;
                reqResp.responseText = item.responseText;
                reqResp.responderUrlText = item.User.userName;
                if (item.submittedBy == item.Request.requestedBy)
                {
                    //reqResp.responderUrlText = requesterUrl;
                    reqResp.responseTitle = item.Request.RequestType1.requestTypeName + ": uppfærsla " + requesterResponses;
                    requesterResponses += 1;
                }
                else
                {
                    reqResp.responseTitle = item.Request.RequestType1.responseTypeName;
                    if (answererResponses != 0)
                    {
                        reqResp.responseTitle += ": uppfærsla " + answererResponses;
                    }
                    answererResponses += 1;
                }
                responses.Add(reqResp);
            }
        }

        public class RequestResponseListItem
        {
            public long responseId { get; set; }
            public string responseText { get; set; }
            public string responseTitle { get; set; }
            public string responderUrlText { get; set; }
        }
    }
}